package Demo2;

import java.util.Arrays;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/1/17
 * Time:16:42
 **/

/**
 * 对于Comparable接口来说，一般是用在类的内部的
 */
class Student implements Comparable<Student>{
    private String name;
    private int score;

    public Student(String name, int score) {
        this.name = name;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        if(this.score>o.score){
            return 1;
        }else if(this.score==o.score){
            return 0;
        }else{
        return -1;
        }
    }
}
public class TestDemo2 {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("a",89);
        students[1] = new Student("b",81);
        students[2] = new Student("c",58);
        System.out.println(Arrays.toString(students));
        //排序
        Arrays.sort(students);
        System.out.println(Arrays.toString(students));//从小到大排序
        //students[0]和students[2]比较
        System.out.println(students[0].compareTo(students[2]));
    }
}
